package com.ushier.shop.fruits.controller;

import com.ushier.shop.fruits.bean.ResponseMessageBean;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/manage/login")
public class ManageLoginServlet extends HttpServlet {

    private static final String USERNAME = "admin";
    private static final String PASSWORD = "admin";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        resp.setHeader("content-type","text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        if(USERNAME.equals(username) && PASSWORD.equals(password)){
            ResponseMessageBean bean = ResponseMessageBean.success();
            out.print(JSONObject.fromObject(bean));
            return;
        }

        ResponseMessageBean bean = ResponseMessageBean.error("用户名/密码错误");
        out.print(JSONObject.fromObject(bean));
    }
}
